#include "Parser.h"
#include "Dictionary.h"

Parser* Parser::inst = 0;

//------------------------------------------------------------------------------

Parser::Parser() : ClassParameter("Root") {
  dictionary = 0;
}

//------------------------------------------------------------------------------

Parser& Parser::instance() {
  if (!inst) inst = new Parser;
  return *inst;
}

//------------------------------------------------------------------------------

int Parser::createDictionaryToken(const char* text) {
  if (dictionary == 0) dictionary = new Dictionary;
  int t = dictionary->createToken(text);
  return t;
}

//------------------------------------------------------------------------------

int Parser::getDictionaryToken(const char* text) const {
  int token = dictionary->getToken(text);
  if (token == -1) {
    fprintf(stderr, "Error: invalid token '%s'\n", text);
    exit(1);
  }
  return token;
}

//------------------------------------------------------------------------------

const char* Parser::getDictionaryWord(int tk) const {
  return dictionary->getWord(tk);
}

//------------------------------------------------------------------------------

void Parser::dumpDictionary() const {
  dictionary->dump();
}

//------------------------------------------------------------------------------

void Parser::print(FILE* fp) const {
  for (size_t i=0; i<subAsgn.size(); ++i) subAsgn[i]->dumpWithComments(fp, "");
}

//------------------------------------------------------------------------------

void Parser::printAssigned(ostream& out) const {

  for (size_t i=0; i<subAsgn.size(); ++i) {
    subAsgn[i]->dumpAssigned(out, "");
  }
}

//------------------------------------------------------------------------------

bool Parser::moduleIsActive(int m) const {
  if (m==-1) return true;
  return (activeModuleTags.find(m) != activeModuleTags.end());
}

//------------------------------------------------------------------------------

void Parser::activateModule(int m) {
  activeModuleTags.insert(m);
}

//------------------------------------------------------------------------------

void Parser::activateModules(const set<int>& m) {
  activeModuleTags.insert(m.begin(),m.end());
}

//------------------------------------------------------------------------------


  
